﻿using Ecommerce.Areas.Manager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ecommerce.Areas.Manager.Controllers
{
    public class CustomerController : Controller
    {

        private DatabaseContext db = new DatabaseContext();
        //
        // GET: /Manager/Customer/

        public ActionResult Index()
        {
            var model = db.Customers.ToList<Customer>();
            return View(model);
        }

        public ActionResult Delete(string cusID)
        {
            int ID = 0;
            try
            {
                ID = Int32.Parse(cusID);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            Customer c = db.Customers.Find(ID);
            if (c.OrderList.Count != 0)
            {
                ViewBag.ErrorMsg = "Can not delete because this customer has order!";
            }
            else
            {
                db.Customers.Remove(c);
                db.SaveChanges();
            }

            var model = db.Customers.ToList<Customer>();

            return View("Index", model);
        }

    }
}
